package com.infocollect.project.api.ui;


import com.infocollect.common.utils.ServletUtils;
import com.infocollect.framework.security.LoginUser;
import com.infocollect.framework.security.service.TokenService;
import com.infocollect.project.api.entity.TappPowerRoleEntity;
import com.infocollect.project.api.helper.CurrentHelp;
import com.infocollect.project.api.helper.JSONUtil;
import com.infocollect.project.api.service.ITappDeptService;
import com.infocollect.project.api.service.ITappOrganService;
import com.infocollect.project.api.service.ITappPowerRoleService;
import com.infocollect.project.api.service.ITappUserService;
import com.infocollect.project.api.vo.CurrentUserInfoVO;
import com.infocollect.project.form.vo.R;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * (TappPowerrole)表控制层
 *
 * @author makejava
 * @since 2019-12-25 10:32:29
 */
@Api(value = "PowerRole", tags = "权限角色信息")
@RestController
@RequestMapping("PowerRole")
public class TappPowerroleController {
    @Autowired
    private TokenService tokenService;
    /**
     * 服务对象
     */
    @Resource
    private ITappPowerRoleService tappPowerroleService;
    /**
     * 服务对象
     */
    @Resource
    private ITappUserService tappUserService;
    /**
     * 服务对象
     */
    @Resource
    private ITappOrganService tappOrganService;
    /**
     * 服务对象
     */
    @Resource
    private ITappDeptService tappDeptService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @PostMapping("selectOne/{id}")
    @ResponseBody
    public R<TappPowerRoleEntity> selectOne(@PathVariable(name = "id") Long id) throws IOException{
        return R.newOk( this.tappPowerroleService.queryById(id));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @PostMapping("del/{id}")
    @ResponseBody
    public R<String> Delete(@PathVariable(name = "id") Long id)throws IOException {
        if(this.tappPowerroleService.deleteById(id)){
            return   R.newOk("操作成功！");
        }else{
            return  R.newError("操作失败！");
        }
    }

    /**
     *
     * @param param
     * @return
     * @throws IOException
     */
    @PostMapping("SaveRoleUser")
    @ResponseBody
    public  R<String> SaveRoleUser(@RequestBody Map<String,Object> param)throws Exception{
        Long roleID =Long.parseLong(param.get("roleID").toString()) ;
        String ParamJson =param.get("ArrNo").toString();
        List<String> strArrNo=  JSONUtil.jsonToObjList(String.class,ParamJson);
        String strType = param.get("Type").toString();
        if (strArrNo==null||strArrNo.size()==0)
        {
            return R.newError("参数为空！");
        }
//        CurrentUserInfoVO currentUserInfo= CurrentHelp.Ins.GetUser();
        LoginUser currentUserInfo = tokenService.getLoginUser(ServletUtils.getRequest());
        for(int i=0;i<strArrNo.size();i++){
            String no=strArrNo.get(i);
            if (no==null&&no.length()==0)
                continue;
            TappPowerRoleEntity powerEntity = new TappPowerRoleEntity();
            powerEntity.setFcreateby(currentUserInfo.getUserName());
            powerEntity.setFlastupdateby(currentUserInfo.getUserName());
            powerEntity.setFcreationdate(new Date());
            powerEntity.setFlastupdatedate(new Date());
            powerEntity.setFpowertype(strType);
            powerEntity.setFroleid(roleID);
            switch (strType)
            {
                case "0":powerEntity.setFlinkid(tappUserService.login(no).getUserId());   break;
                case "1": powerEntity.setFlinkid(tappDeptService.queryByCode(no).getFid());  break;
                case "2": powerEntity.setFlinkid(tappOrganService.queryByCode(no).getFid()); break;
                default: break;
            }
            tappPowerroleService.insert(powerEntity);
        }
        return R.newOk();
    }
}